<?php
namespace app\admin\model;

use think\Config;
use think\Db;
use think\Model;
use think\Session;

class OrdersModel extends Model
{
	/**
	*	获取全部商品信息
	*/
	public function getGoods()
	{
		$result = Db::table('sz_goods')->select();
		return $result;
	}

	/**
	*	获取所有会员信息
	*/
	public function getUsers()
	{
		$result = Db::table('sz_user')->select();
		return $result;
	}

	/**
	*	获取每个会员的收货地址
	*/
	public function getUsersAddress($uid)
	{
		if($uid){

			$result = Db::table('sz_address')
					->where('uid',$uid)
					->select();
			
			return $result;
		}
		
	}

	/**
	*	订单列表
	*/
	public function getOrders($data)
	{
		$where = [];

		/* 订单号 */
		$o_code = empty($data['o_code']) ? '' : $data['o_code'];
		$o_code = trim($o_code);
		if(!empty($o_code)){
			$where['o.o_code'] = $o_code;
		}

		/* 会员姓名 */
		$u_name = empty($data['u_name']) ? '' : $data['u_name'];
		$u_name = trim($u_name);
		if(!empty($u_name)){
			$where['u.u_name'] = $u_name;
		}

		/* 状态 */
		$o_status = empty($data['o_status']) ? '' : $data['o_status'];
		$o_status = trim($o_status);
		if(!empty($o_status)){
			$where['o.o_status'] = $o_status;
		}

		/* 付款方式 */
		$o_type = empty($data['o_type']) ? '' : $data['o_type'];
		$o_type = trim($o_type);
		if(!empty($o_type)){
			$where['o.o_type'] = $o_type;
		}

		/* 物流公司 */
		$o_company = empty($data['o_company']) ? '' : $data['o_company'];
		$o_company = trim($o_company);
		if(!empty($o_company)){
			$where['o.o_company'] = $o_company;
		}

		/* 物流单号 */
		$o_snumber = empty($data['o_snumber']) ? '' : $data['o_snumber'];
		$o_snumber = trim($o_snumber);
		if(!empty($o_snumber)){
			$where['o.o_snumber'] = $o_snumber;
		}

		$join = [
			['sz_user u','o.uid=u.id','left'],
			['sz_address a','o.aid=a.id','left']
		];

		$result = Db::table('sz_orders')->alias('o')
				->join($join)
				->field('o.*,u.u_name,a.a_address')
				->where($where)
				->order('o.id ASC')
				->paginate(20,false,['query' => request()->param()]);

		return $result;
	}


	/**
	*	订单列表-商品信息
	* @param $gids  商品的id集  
	*/
	public function getOrderGoods($gids)
	{
		if($gids){
			$result = Db::table('sz_goods')->where('id','in',$gids)->select();
			$data = [];
			foreach($result as $key=>$val){
				$list[$key] = json_decode($val['g_pic'],true);
				foreach($list as $k1=>$v1){
					$res[$key]['photos'] = $v1['photos'];
					foreach($res[$key]['photos'] as $k2=>$v2){
						$arr['photos'][$k2]['url'] = getPreviewUrl($v2['url']);
						
					}
				}
				$data[$key]['id'] = $val['id'];
				$data[$key]['g_type'] = $val['g_type'];
				$data[$key]['g_name'] = $val['g_name'];
				$data[$key]['g_cprice'] = $val['g_cprice'];
				$data[$key]['g_oprice'] = $val['g_oprice'];
				$data[$key]['g_description'] = $val['g_description'];
				$data[$key]['g_content'] = $val['g_content'];
				$data[$key]['g_pic'] = $arr;
				$data[$key]['g_show'] = $val['g_show'];
				$data[$key]['g_tnumber'] = $val['g_tnumber'];
				$data[$key]['g_unit'] = $val['g_unit'];
				$data[$key]['g_number'] = $val['g_number'];
				$data[$key]['starttime'] = $val['starttime'];
				$data[$key]['endtime'] = $val['endtime'];
				$data[$key]['lefttime'] = $val['endtime'] - strtotime(date('Y-m-d H:i:s',time()));
				$data[$key]['addtime'] = $val['addtime'];
			}

			return $data;

		}
	}

	/**
	*	订单列表-商品规格信息
	*	@param  $gid  商品id
	*	@param  $oid  订单id
	*/
	public function getorderGoodsInfo($gid,$oid)
	{
		$res = Db::table('sz_orderinfo')->alias('oi')
			 ->join('sz_info i','oi.iid=i.id','left')
			 ->field('oi.*,i.i_name,i.i_price,i.i_number,i.i_commission')
			 ->where('oi.gid',$gid)
			 ->where('oi.oid',$oid)
			 ->select();
		return $res;
	}

	/**
	*	获取一个订单
	*/
	public function getOne($id)
	{
		if($id){
			$result = Db::table('sz_orders')->where('id',$id)->find();
			return $result;
		}
	}

	/**
	*	修改订单信息
	*/
	public function editOrders($id,$data)
	{
		$data['o_time'] = empty($data['o_time']) ? 0 : strtotime(trim($data['o_time']));

		$result = Db::table('sz_orders')->where('id',$id)->update($data);

		return $result;
	}

	/**
	*	删除订单
	*/
	public function deleteOrders($id)
	{
		if($id){
			$result = Db::table('sz_orders')->where('id',$id)->delete();
			return $result;
		}else{
			return false;
		}
	}

}